using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Posts : BasePage
{
    private enum Modes
    {
        CreateHomePage,
        SelectPost,
        PostDetailsSelected,
        EditPost,
        AddChildPost,
        PostContentsSelected,
        PostTopicsSelected,
        PostMenusSelected,
        Publishing
    }

    #region Page Events
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dsSiteMap2.SiteId = AcmsSite.Id;
            tvSiteMap2.DataBind();
            if (tvSiteMap2.Nodes.Count == 0)
            {
                SetMode(Modes.CreateHomePage);
            }
            else
            {
                SetMode(Modes.SelectPost);
            }
            /*tvSiteMap.DataBind();
            if (tvSiteMap.Nodes.Count == 0)
            {
                divNoHomePage.Visible = true;
                dvPost.ChangeMode(DetailsViewMode.Insert);
                divDetails.Visible = true;
            }*/
        }
    }

    private void SetMode(Modes mode)
    {
        //hide buttons
        btnAddNewPost.Visible = false;
        btnSelectPost.Visible = false;
        btnEditPost.Visible = false;
        btnSavePost.Visible = false;
        btnCancelPost.Visible = false;
        btnAddChildPost.Visible = false;
        btnNewVersion.Visible = false;
        btnSaveTopics.Visible = false;
        btnSaveMenus.Visible = false;   
        //hide divs
        divSiteMap.Visible = false;
        divNoHomePage.Visible = false;
        divDetails.Visible = false;

        switch (mode)
        {
            case Modes.CreateHomePage:
                divNoHomePage.Visible = true;
                divDetails.Visible = true;
                dvPost.ChangeMode(DetailsViewMode.Insert);
                btnSavePost.Visible = true;
                litPageTitle.Text = "New Home Page";
                break;
            case Modes.SelectPost:
                divSiteMap.Visible = true;
                btnAddNewPost.Visible = true;
                break;
            case Modes.PostDetailsSelected:
                if (dvPost.CurrentMode != DetailsViewMode.ReadOnly)
                    dvPost.ChangeMode(DetailsViewMode.ReadOnly);
                divDetails.Visible = true;
                btnEditPost.Visible = true;
                btnSelectPost.Visible = true;
                btnAddChildPost.Visible = true;
                multiPage.SelectedIndex = 0;
                tabs.SelectedIndex = 0;
                EnableTabs();
                litPageTitle.Text = CurrentPageTitle;
                break;
            case Modes.EditPost:
                if (dvPost.CurrentMode != DetailsViewMode.Edit)
                    dvPost.ChangeMode(DetailsViewMode.Edit);
                divDetails.Visible = true;
                btnSavePost.Visible = true;
                btnCancelPost.Visible = true;
                DisableTabs();
                break;
            case Modes.AddChildPost:
                if (dvPost.CurrentMode != DetailsViewMode.Insert)
                    dvPost.ChangeMode(DetailsViewMode.Insert);
                divDetails.Visible = true;
                btnSavePost.Visible = true;
                btnCancelPost.Visible = true;
                multiPage.SelectedIndex = 0;
                tabs.SelectedIndex = 0;
                DisableTabs();
                break;
            case Modes.PostContentsSelected:
                btnSelectPost.Visible = true;
                divDetails.Visible = true;
                EnableTabs();
                break;
            case Modes.PostTopicsSelected:
                btnSelectPost.Visible = true;
                btnSaveTopics.Visible = true;
                divDetails.Visible = true;
                EnableTabs();
                break;
            case Modes.PostMenusSelected:
                btnSelectPost.Visible = true;
                btnSaveMenus.Visible = true;
                divDetails.Visible = true;
                EnableTabs();
                break;
            case Modes.Publishing:
                btnSelectPost.Visible = true;
                divDetails.Visible = true;
                EnableTabs();
                break;
        }
    }

    private void EnableTabs()
    {
        foreach (Telerik.WebControls.Tab tab in tabs.Tabs)
        {
            tab.Enabled = true;
        }
    }

    private void DisableTabs()
    {
        foreach (Telerik.WebControls.Tab tab in tabs.Tabs)
        {
            if (!tab.Selected)
                tab.Enabled = false;
        }
    }

    protected override object SaveViewState()
    {
        ViewState["CurrentId"] = CurrentId;
        ViewState["CurrentVersionId"] = CurrentVersionId;
        ViewState["CurrentPageTitle"] = CurrentPageTitle;
        ViewState["ContentControlPaths"] = contentControlPaths;
        ViewState["AddPostingParentId"] = addPostingParentId;
        ViewState["UnusedControlPaths"] = unusedControlPaths;
        return base.SaveViewState();
    }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        CurrentId = ViewState["CurrentId"] as int?;
        CurrentVersionId = ViewState["CurrentVersionId"] as int?;
        CurrentPageTitle = ViewState["CurrentPageTitle"] as string;
        contentControlPaths = ViewState["ContentControlPaths"] as Hashtable;
        addPostingParentId = (int)ViewState["AddPostingParentId"];
        unusedControlPaths = ViewState["UnusedControlPaths"] as Hashtable;
    }
    #endregion

    #region Tree View
    //protected void tvSiteMap_SelectedNodeChanged(object sender, EventArgs e)
    //{
    //    /*dvPost.ChangeMode(DetailsViewMode.ReadOnly);
    //    currentId = int.Parse(tvSiteMap.SelectedNode.Value);
    //    divDetails.Visible = true;
    //    divMenus.Visible = true;
    //    divControls.Visible = true;
    //    divTopics.Visible = true;
    //    divUnusedControls.Visible = true;

    //    dvPost.DataBind();
    //    cblMenus.DataBind();
    //    cblTopics.DataBind();
    //    dlPostControls.EditItemIndex = -1;
    //    dlPostControls.DataBind();
    //    dlUnused.DataBind();*/

    //}

    //protected void tvSiteMap_TreeNodeDataBound(object sender, TreeNodeEventArgs e)
    //{
    //    /*if (currentId != null)
    //    {
    //        if (e.Node.Value == currentId.Value.ToString())
    //        {
    //            e.Node.Selected = true;
    //        }
    //    }*/
    //}
    #endregion

    #region page properties
    private int? currentId;
    protected int? CurrentId
    {
        get { return currentId; }
        set { currentId = value; }
    }
    
    private int? currentVersionId;
    protected int? CurrentVersionId
    {
        get { return currentVersionId; }
        set { currentVersionId = value; }
    }

    private string currentPageTitle;
    protected string CurrentPageTitle
    {
        get { return currentPageTitle; }
        set { currentPageTitle = value; }
    }
    private List<Acms.Bll.PostMenu> menus;
    protected List<Acms.Bll.PostMenu> Menus
    {
        get
        {
            if (menus == null && CurrentVersionId != null)
            {
                menus = Acms.Bll.PostMenu.GetAllByPostVersionId(CurrentVersionId.Value);
            }
            return menus;
        }
    }

    private List<Acms.Bll.PostTopic> topics;
    protected List<Acms.Bll.PostTopic> Topics
    {
        get
        {
            if (topics == null && CurrentVersionId != null)
            {
                topics = Acms.Bll.PostTopic.GetAllByPostVersionId(CurrentVersionId.Value);
            }
            return topics;
        }
    }

    private List<Acms.Bll.PostControl> GetPostControls()
    {
        if (CurrentVersionId != null)
            return Acms.Bll.PostControl.GetAllByPostVersionId(CurrentVersionId.Value);
        else
            return null;
    }

    private Hashtable unusedControlPaths = new Hashtable();

    private Hashtable contentControlPaths = new Hashtable();

    private int addPostingParentId;
    #endregion

    #region DataSource
    protected void dsPost_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        if (CurrentVersionId != null)
        {
            e.SetData(Acms.Bll.Post.GetByVersionId(CurrentVersionId.Value));
        }
    }

    protected void dsPost_UpdatingData(object sender, Acms.DataControls.UpdatingDataEventArgs<Acms.Bll.Post> e)
    {
        if (e.Action == Acms.DataControls.UpdateAction.Insert)
        {
            if (this.CurrentVersionId != null)
            {
                Acms.Bll.Post post = Acms.Bll.Post.GetByVersionId(CurrentVersionId.Value);
                e.Item.ParentId = post.Id;
            }
            e.Item.SiteId = AcmsSite.Id;
        }
        FileUpload fuThumb = dvPost.FindControl("fuThumb") as FileUpload;
        if (fuThumb != null && fuThumb.HasFile)
        {
            Acms.Bll.File thumb = Acms.Bll.File.CreateInstance(Acms.Bll.File.FileTypes.Image, fuThumb.PostedFile, fuThumb.FileBytes);
            thumb.BeginTransaction();
            thumb.Update();
            e.Item.ThumbnailId = thumb.Id;
            e.Item.UseTransaction(thumb.TransactionKey);
        }
    }

    protected void dsPost_UpdatedData(object sender, Acms.DataControls.UpdatingDataEventArgs<Acms.Bll.Post> e)
    {
        if (e.Item.TransactionKey != Guid.Empty)
        {
            e.Item.CommitTransaction();
        }
        CurrentId = e.Item.Id;
        CurrentVersionId = e.Item.VersionId;
        CurrentPageTitle = e.Item.Path.Replace("~", AcmsSite.Root);
    }


    protected void dsTemplates_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Template> e)
    {
        e.SetData(Acms.Bll.Template.GetAllBySiteId(AcmsSite.Id));
    }

    protected void dsMenus_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Menu> e)
    {
        e.SetData(Acms.Bll.Menu.GetAllBySiteId(AcmsSite.Id));
    }

    protected void dsPostControls_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.PostControl> e)
    {
        e.SetData(GetPostControls());
    }

    protected void dsTopics_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Topic> e)
    {
        e.SetData(Acms.Bll.Topic.GetAllBySiteId(AcmsSite.Id));
    }
    #endregion

    #region details view
    protected void dvPost_NameTextBoxDataBound(object sender, Acms.WebControls.TextboxDataboundEventArgs e)
    {
        if (e.DataItem == null && this.CurrentVersionId == null)
        {
            //inserting new home page
            e.TextBox.Text = "~";
            e.TextBox.Enabled = false;
        }
        else if (e.DataItem != null && e.DataItem is Acms.Bll.Post && ((Acms.Bll.Post)e.DataItem).ParentId == null)
        {
            //editing home page so disable name
            e.TextBox.Enabled = false;
        }
    }

    //protected void dvPost_DataBound(object sender, EventArgs e)
    //{
    //    if(dvPost.CurrentMode == DetailsViewMode.Insert)
    //    {
    //        if (CurrentVersionId == null)
    //        {
    //           //Creating HomePage so diable name field
    //            TextBox txtNameInsert = dvPost.Fields[0]..FindControl("txtNameInsert") as TextBox;
    //           if (txtNameInsert != null)
    //           {
    //               txtNameInsert.Text = "~";
    //               txtNameInsert.Enabled = false;
    //           }
    //      }
    //   }
    //    else if(dvPost.CurrentMode == DetailsViewMode.Edit)
    //    {
    //        if (tvSiteMap.SelectedNode.Parent == null)
    //        {
    //            TextBox txtNameEdit = dvPost.FindControl("txtNameEdit") as TextBox;
    //            if (txtNameEdit != null)
    //            {
    //                txtNameEdit.Enabled = false;
    //            }
    //        }
    //    }
    //    else if (dvPost.CurrentMode == DetailsViewMode.ReadOnly)
    //    {
    //        LinkButton btnMoveUp = dvPost.FindControl("btnMoveUp") as LinkButton;
    //        if (btnMoveUp != null)
    //        {
    //            btnMoveUp.Visible = tvSiteMap.SelectedNode.Parent != null && tvSiteMap.SelectedNode.Parent.ChildNodes[0] != tvSiteMap.SelectedNode;
    //        }

    //        LinkButton btnMoveParent = dvPost.FindControl("btnMoveParent") as LinkButton;
    //        if (btnMoveParent != null)
    //        {
    //            btnMoveParent.Visible = tvSiteMap.SelectedNode != null && tvSiteMap.SelectedNode.Parent != null && tvSiteMap.SelectedNode.Parent.Parent != null;
    //        }

    //        LinkButton btnAddSibling = dvPost.FindControl("btnAddSibling") as LinkButton;
    //        if (btnAddSibling != null)
    //        {
    //            btnAddSibling.Visible = tvSiteMap.SelectedNode != null && tvSiteMap.SelectedNode.Parent != null;
    //        }

    //        Repeater rptSiblings = dvPost.FindControl("rptSiblings") as Repeater;
    //        if (rptSiblings != null && tvSiteMap.SelectedNode != null)
    //        {
    //            TreeNodeCollection tmp = new TreeNodeCollection();
    //            if (tvSiteMap.SelectedNode != null && tvSiteMap.SelectedNode.Parent != null)
    //            {
    //                foreach (TreeNode node in tvSiteMap.SelectedNode.Parent.ChildNodes)
    //                {
    //                    if (node != tvSiteMap.SelectedNode)
    //                    {
    //                        tmp.Add(new TreeNode(node.Text, node.Value));
    //                    }
    //                }
    //                if (tmp.Count > 0)
    //                {
    //                    rptSiblings.DataSource = tmp;
    //                    rptSiblings.DataBind();
    //                }
    //            }
    //        }
    //    }
    //}

    //protected void dvPost_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    //{
    //    if (e.Exception == null)
    //    {
    //        /*addPostingParentId = 0;
    //        divNoHomePage.Visible = false;
    //        currentVersionId = (int)e.Values[Acms.DataControls.PostDataSource.Fields.VersionId.ToString()];

    //        tvSiteMap.DataBind();
    //        cblMenus.DataBind();
    //        cblTopics.DataBind();
    //        dlPostControls.EditItemIndex = -1;
    //        dlPostControls.DataBind();
    //        dlUnused.DataBind();*/
    //    }
    //}

    //protected void dvPost_ItemDeleted(object sender, DetailsViewDeletedEventArgs e)
    //{
    //    if (e.Exception == null)
    //    {
    //        /*if (tvSiteMap.SelectedNode.Parent == null)
    //        {
    //            currentId = null;
    //            divNoHomePage.Visible = true;
    //            dvPost.ChangeMode(DetailsViewMode.Insert);
    //        }
    //        else
    //        {
    //            tvSiteMap.SelectedNode.Parent.Select();
    //            currentId = int.Parse(tvSiteMap.SelectedNode.Value);
    //        }
    //        tvSiteMap.DataBind();
    //        cblMenus.DataBind();
    //        cblTopics.DataBind();
    //        dlPostControls.EditItemIndex = -1;
    //        dlPostControls.DataBind();
    //        dlUnused.DataBind();*/
    //    }
    //}

    //protected void btnMoveUp_Click(object sender, EventArgs e)
    //{
    //    if (CurrentVersionId.HasValue && tvSiteMap.SelectedNode != null && tvSiteMap.SelectedNode.Parent != null)
    //    {
    //        List<Acms.Bll.Post> tmp = Acms.Bll.Post.GetAllByParentId(int.Parse(tvSiteMap.SelectedNode.Parent.Value));
    //        for (int i = 1; i < tmp.Count; i++)
    //        {
    //            if (tmp[i].Id == CurrentVersionId.Value)
    //            {
    //                tmp[i].Sequence = i - 1;
    //                tmp[i - 1].Sequence = i;
    //                tmp[i].Update();
    //                tmp[i - 1].Update();
    //            }
    //        }
    //        tvSiteMap.DataBind();
    //        dvPost.DataBind();
    //    }
    //}

    //protected void btnMoveParent_Click(object sender, EventArgs e)
    //{
    //    if (CurrentVersionId.HasValue && tvSiteMap.SelectedNode != null && tvSiteMap.SelectedNode.Parent != null && tvSiteMap.Parent.Parent != null)
    //    {
    //        Acms.Bll.Post tmp = Acms.Bll.Post.GetByVersionId(CurrentVersionId.Value);
    //        if (tmp != null)
    //        {
    //            tmp.ParentId = int.Parse(tvSiteMap.SelectedNode.Parent.Parent.Value);
    //            tmp.Sequence = tvSiteMap.SelectedNode.Parent.Parent.ChildNodes.Count;
    //            tmp.Update();
    //            tvSiteMap.DataBind();
    //            dvPost.DataBind();
    //        }
    //    }
    //}

    //protected void rptSiblings_ItemCommand(object source, RepeaterCommandEventArgs e)
    //{
    //    if (e.CommandName == "MakeChild")
    //    {
    //        Acms.Bll.Post tmp = Acms.Bll.Post.GetByVersionId(CurrentVersionId.Value);
    //        if (tmp != null)
    //        {
    //            tmp.ParentId = int.Parse(e.CommandArgument.ToString());
    //            foreach (TreeNode node in tvSiteMap.SelectedNode.Parent.ChildNodes)
    //            {
    //                if (node.Value == tmp.ParentId.Value.ToString())
    //                {
    //                    tmp.Sequence = node.ChildNodes.Count;
    //                }
    //            }
    //            tmp.Update();
    //            tvSiteMap.DataBind();
    //            dvPost.DataBind();
    //        }
    //    }
    //}

    //protected void dvPost_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
    //{
    //    tvSiteMap.DataBind();
    //    cblMenus.DataBind();
    //    cblTopics.DataBind();
    //    dlPostControls.EditItemIndex = -1;
    //    dlPostControls.DataBind();
    //    dlUnused.DataBind();
    //}
    #endregion

    #region Menus

    protected void btnSaveMenus_Click(object sender, EventArgs e)
    {
        foreach (CheckBox item in cblMenus.Items)
        {
            bool found = false;
            int id = (int)cblMenus.GetValue(item);
            foreach (Acms.Bll.PostMenu menu in Menus)
            {
                if (menu.MenuId == id)
                {
                    if (menu.Included != item.Checked)
                    {
                        menu.Included = item.Checked;
                        menu.Update();
                    }
                    found = true;
                    break;
                }
            }
            if (!found)
            {
                Acms.Bll.PostMenu menu = Acms.Bll.PostMenu.CreateInstance(CurrentId.Value, id, item.Checked);
                menu.Update();
            }
        }
        menus = null;
    }

    //private void SetMenuCheckBoxes()
    //{
    //    foreach (Acms.Bll.PostMenu menu in Menus)
    //    {
    //        foreach (CheckBox item in cblMenus.Items)
    //        {
    //            if ((int)cblMenus.GetValue(item) == menu.MenuId)
    //            {
    //                item.Checked = menu.Included;
    //            }
    //        }
    //    }
    //}

    protected void cblMenus_ItemDataBound(object sender, Acms.WebControls.CheckBoxList.ItemDataBoundEventArgs e)
    {
        if (e.Value is int)
        {
            int id = (int)e.Value;
            foreach (Acms.Bll.PostMenu menu in Menus)
            {
                if (menu.MenuId == id)
                {
                    e.Item.Checked = menu.Included;
                }
            }
        }
    }
    #endregion

    #region ContentControls
    protected void dlPostControls_EditCommand(object source, DataListCommandEventArgs e)
    {
        dlPostControls.EditItemIndex = e.Item.ItemIndex;
        dlPostControls.DataBind();
    }

    protected void dlPostControls_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.EditItem || e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Acms.Bll.PostControl postCtrl = e.Item.DataItem as Acms.Bll.PostControl;
            Label lblError = e.Item.FindControl("lblError") as Label;
            PlaceHolder phControl = e.Item.FindControl("phControl") as PlaceHolder;
            if (!string.IsNullOrEmpty(postCtrl.AdminControlType))
            {
                try
                {
                    Acms.AcmsControls.BaseContentControlEdit ctrl = Page.LoadControl(System.Type.GetType(postCtrl.AdminControlType, true, true), null) as Acms.AcmsControls.BaseContentControlEdit;
                    if (ctrl != null)
                    {
                        ctrl.State = e.Item.ItemType == ListItemType.EditItem ? Acms.AcmsControls.BaseContentControlEdit.States.Edit : Acms.AcmsControls.BaseContentControlEdit.States.Display;
                        contentControlPaths[e.Item.ItemIndex] = postCtrl.AdminControlType;
                        phControl.Controls.Add(ctrl);
                        ctrl.LoadData(postCtrl);
                        ctrl.DataBind();
                    }
                    else
                    {
                        lblError.Text = string.Format("Faild to load control '{0}' as a 'Acms.AcmsControls.BaseAdminContentControl", postCtrl.AdminControlType);
                        lblError.Visible = true;
                    }
                }
                catch (System.IO.FileNotFoundException ex)
                {
                    lblError.Text = ex.Message;
                    lblError.Visible = true;
                }
            }
        }
    }

    protected void dlPostControls_ItemCreated(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.EditItem || e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            PlaceHolder phControl = e.Item.FindControl("phControl") as PlaceHolder;
            if (phControl != null && contentControlPaths[e.Item.ItemIndex] is string)
            {
                Control ctrl = Page.LoadControl(System.Type.GetType(contentControlPaths[e.Item.ItemIndex] as string, true, true), null);
                if (ctrl != null)
                {
                    phControl.Controls.Add(ctrl);
                }
            }
        }
    }

    protected void dlPostControls_DataBinding(object sender, EventArgs e)
    {
        contentControlPaths = new Hashtable();
    }

    protected void dlPostControls_CancelCommand(object source, DataListCommandEventArgs e)
    {
        dlPostControls.EditItemIndex = -1;
        dlPostControls.DataBind();
    }

    protected void dlPostControls_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        PlaceHolder phControl = e.Item.FindControl("phControl") as PlaceHolder;
        Acms.Bll.PostControl postControl = GetPostControls()[e.Item.ItemIndex];
        Acms.AcmsControls.BaseContentControlEdit ctrl = phControl.Controls[0] as Acms.AcmsControls.BaseContentControlEdit;

        if (ctrl != null)
        {
            ctrl.SaveData(postControl);
        }
        dlPostControls.EditItemIndex = -1;
        dlPostControls.DataBind();
    }

    protected void dlPostControls_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        PlaceHolder phControl = e.Item.FindControl("phControl") as PlaceHolder;
        Acms.Bll.PostControl postControl = GetPostControls()[e.Item.ItemIndex];
        Acms.AcmsControls.BaseContentControlEdit ctrl = phControl.Controls[0] as Acms.AcmsControls.BaseContentControlEdit;
        if (ctrl != null)
        {
            ctrl.DeleteData(postControl);
        }

        dlPostControls.EditItemIndex = -1;
        dlPostControls.DataBind();
    }

    #endregion

    protected void cblTopics_ItemDataBound(object sender, Acms.WebControls.CheckBoxList.ItemDataBoundEventArgs e)
    {
        Acms.Bll.Topic topic = e.DataItem as Acms.Bll.Topic;
        if(topic != null)
        {
            foreach (Acms.Bll.PostTopic postTopic in Topics)
            {
                if (postTopic.TopicId == topic.Id)
                {
                    e.Item.Checked = true;
                }
            }
        }
    }

    protected void btnSaveTopics_Click(object sender, EventArgs e)
    {
        foreach (CheckBox item in cblTopics.Items)
        {
            bool included = false;
            int id = (int)cblTopics.GetValue(item);
            foreach (Acms.Bll.PostTopic topic in Topics)
            {
                if (topic.TopicId == id)
                {
                    included = true;
                }
            }
            if (included && !item.Checked)
            {
                foreach (Acms.Bll.PostTopic topic in Topics)
                {
                    if (topic.TopicId == id)
                    {
                        topic.Delete();
                    }
                }
            }
            else if (!included && item.Checked)
            {
                Acms.Bll.PostTopic topic = Acms.Bll.PostTopic.CreateInstance(CurrentVersionId.Value, id);
                topic.Update();
            }
        }
        topics = null;
    }

    //protected void dvPost_ItemCommand(object sender, DetailsViewCommandEventArgs e)
    //{
    //    if (e.CommandName == "AddSibling")
    //    {
    //        addPostingParentId = int.Parse(tvSiteMap.SelectedNode.Parent.Value);
    //        dvPost.ChangeMode(DetailsViewMode.Insert);
    //        dvPost_ModeChanged(dvPost, EventArgs.Empty);
    //    }
    //    else if (e.CommandName == "AddChild")
    //    {
    //        addPostingParentId = int.Parse(tvSiteMap.SelectedNode.Value);
    //        dvPost.ChangeMode(DetailsViewMode.Insert);
    //        dvPost_ModeChanged(dvPost, EventArgs.Empty);
    //    }
    //}
    //protected void dvPost_ModeChanged(object sender, EventArgs e)
    //{
    //    if (dvPost.CurrentMode == DetailsViewMode.Insert)
    //    {
    //        divControls.Visible = false;
    //        divMenus.Visible = false;
    //        divTopics.Visible = false;
    //        divUnusedControls.Visible = false;
    //    }
    //    else
    //    {
    //        divControls.Visible = true;
    //        divMenus.Visible = true;
    //        divTopics.Visible = true;
    //        divUnusedControls.Visible = true;
    //    }
    //}

    //protected void dsUnused_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.UnusedPostControl> e)
    //{
    //    if (CurrentVersionId != null)
    //    {
    //        e.SetData(Acms.Bll.UnusedPostControl.GetAllByPostVersionId(CurrentVersionId.Value));
    //    }
    //}


    //protected void dlUnused_ItemCreated(object sender, DataListItemEventArgs e)
    //{
    //    if (e.Item.ItemType == ListItemType.EditItem || e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    //    {
    //        PlaceHolder phControl = e.Item.FindControl("phControl") as PlaceHolder;
    //        if (phControl != null && unusedControlPaths[e.Item.ItemIndex] is string)
    //        {
    //            Control ctrl = Page.LoadControl(System.Type.GetType(unusedControlPaths[e.Item.ItemIndex] as string, true, true), null);
    //            if (ctrl != null)
    //            {
    //                phControl.Controls.Add(ctrl);
    //            }
    //        }
    //    }
    //}

    //protected void dlUnused_ItemDataBound(object sender, DataListItemEventArgs e)
    //{
    //    if (e.Item.ItemType == ListItemType.EditItem || e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    //    {
    //        Acms.Bll.UnusedPostControl postCtrl = e.Item.DataItem as Acms.Bll.UnusedPostControl;
    //        Label lblError = e.Item.FindControl("lblError") as Label;
    //        PlaceHolder phControl = e.Item.FindControl("phControl") as PlaceHolder;
    //        if (!string.IsNullOrEmpty(postCtrl.AdminControlType))
    //        {
    //            try
    //            {
    //                Acms.AcmsControls.BaseContentControlEdit ctrl = Page.LoadControl(System.Type.GetType(postCtrl.AdminControlType, true, true), null) as Acms.AcmsControls.BaseContentControlEdit;
    //                if (ctrl != null)
    //                {
    //                    ctrl.State = Acms.AcmsControls.BaseContentControlEdit.States.Display;
    //                    unusedControlPaths[e.Item.ItemIndex] = postCtrl.AdminControlType;
    //                    phControl.Controls.Add(ctrl);
    //                    ctrl.LoadData(postCtrl);
    //                    ctrl.DataBind();
    //                }
    //                else
    //                {
    //                    lblError.Text = string.Format("Faild to load control '{0}' as a 'Acms.AcmsControls.BaseAdminContentControl", postCtrl.AdminControlType);
    //                    lblError.Visible = true;
    //                }
    //            }
    //            catch (System.IO.FileNotFoundException ex)
    //            {
    //                lblError.Text = ex.Message;
    //                lblError.Visible = true;
    //            }
    //        }
    //    }
    //}

    //protected void dlUnused_DataBinding(object sender, EventArgs e)
    //{
    //    unusedControlPaths = new Hashtable();
    //}
    protected void btnSavePost_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            if (dvPost.CurrentMode == DetailsViewMode.Insert)
            {
                dvPost.InsertItem(true);
            }
            else if (dvPost.CurrentMode == DetailsViewMode.Edit)
            {
                dvPost.UpdateItem(true);
            }
        }
    }

    protected void btnEditPost_Click(object sender, EventArgs e)
    {
        SetMode(Modes.EditPost);
    }

    protected void btnCancelPost_Click(object sender, EventArgs e)
    {
        SetMode(Modes.PostDetailsSelected);
    }

    protected void tvSiteMap2_NodeBound(object o, Telerik.WebControls.RadTreeNodeEventArgs e)
    {
        if (e.NodeBound.Parent == null)
        {
            e.NodeBound.DragEnabled = false;
        }
    }

    protected void tvSiteMap2_NodeClick(object o, Telerik.WebControls.RadTreeNodeEventArgs e)
    {
        CurrentVersionId = int.Parse(e.NodeClicked.Value);
        Acms.Bll.Post tmp = Acms.Bll.Post.GetByVersionId(CurrentVersionId.Value);
        if (tmp != null)
        {
            CurrentId = tmp.Id;
            CurrentPageTitle = tmp.Path.Replace("~", AcmsSite.Root);
        }
        tabVersions.SelectedIndex = 0;
        tabVersions.DataBind();

        tabs.SelectedIndex = 0;

        SetMode(Modes.PostDetailsSelected);
        DataBindPageShowing();
    }

    protected void dvPost_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
        if (e.Exception == null)
        {
            SetMode(Modes.PostDetailsSelected);
        }
    }

    protected void dvPost_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
    {
        if (e.Exception == null)
        {
            SetMode(Modes.PostDetailsSelected);
        }
    }
    protected void btnSelectPost_Click(object sender, EventArgs e)
    {
        SetMode(Modes.SelectPost);
    }

    protected void btnAddChildPost_Click(object sender, EventArgs e)
    {
        SetMode(Modes.AddChildPost);
    }

    protected void tabs_TabClick(object sender, Telerik.WebControls.TabStripEventArgs e)
    {
        if (e.Tab.PageViewID == multiPage.PageViews[0].ID)
        {
            SetMode(Modes.PostDetailsSelected);
        }
        else if (e.Tab.PageViewID == multiPage.PageViews[1].ID)
        {
            SetMode(Modes.PostContentsSelected);
        }
        else if (e.Tab.PageViewID == multiPage.PageViews[2].ID)
        {
            SetMode(Modes.PostTopicsSelected);
        }
        else if (e.Tab.PageViewID == multiPage.PageViews[3].ID)
        {
            SetMode(Modes.PostMenusSelected);
        }
        else if (e.Tab.PageViewID == multiPage.PageViews[4].ID)
        {
            SetMode(Modes.Publishing);
        }
        else
        {
            throw new Exception("Tab not handled");
        }
    }

    protected void dsVersions_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        if (CurrentId != null)
        {
            e.SetData(Acms.Bll.Post.GetAllFutureVersionsById(CurrentId.Value));
        }
    }
    protected void btnPublish_Click(object sender, EventArgs e)
    {
        Acms.Bll.Post post = Acms.Bll.Post.GetByVersionId(CurrentVersionId.Value);
        post.Publish();
    }

    protected void btnNewVersion_Click(object sender, EventArgs e)
    {
        Acms.Bll.Post post = Acms.Bll.Post.CreatePendingVersion(CurrentId.Value);
        CurrentVersionId = post.VersionId;
        tabVersions.DataBind();
        SetMode(Modes.PostDetailsSelected);
    }

    protected void ddlRelease_SelectedIndexChanged(object sender, EventArgs e)
    {
        tvSiteMap2.DataBind();
    }

    protected void dsSiteMap2_GetVersionTimeStamp(object sender, Acms.DataControls.GetVersionTimeStampEventArguments e)
    {
        if (ddlRelease.SelectedValue == "Live")
        {
            e.SetLive();
        }
        else
        {
            e.SetPending();
        }
    }

    protected void tabVersions_TabCreated(object sender, Telerik.WebControls.TabStripEventArgs e)
    {
        if (currentVersionId != null && e.Tab.Value == currentVersionId.ToString())
        {
            e.Tab.Selected = true;
        }
    }

    protected void tabVersions_TabClick(object sender, Telerik.WebControls.TabStripEventArgs e)
    {
        try
        {
            CurrentVersionId = int.Parse(e.Tab.Value);
        }
        catch(FormatException)
        {
            return;
        }
        DataBindPageShowing();
    }

    private void DataBindPageShowing()
    {
        if (pageDetails.Visible)
        {
            pageDetails.DataBind();
        }
        else if (pageContent.Visible)
        {
            pageContent.Visible = true;
        }
        /*if (pagePublish.Visible)
        {
        }*/
    }
}
